﻿using System;
using System.IO;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Replayer.Model;

namespace Replayer.WinForms.Ui.Gui {
    public partial class CreateNewCompilationWizard : XtraForm {
        public CreateNewCompilationWizard() {
            InitializeComponent();
            wizardControl1.FinishButtonClick += wizardControl1_FinishButtonClick;
        }

        private void wizardControl1_FinishButtonClick(object sender, EventArgs e) {
            //generate the compilation with the tracks.
            ICompilation compilation = CompilationFactory.CreateNew();
            compilation.Title = _tbCollectionName.Text;

            //create tracks
            foreach (string trackMediaFile in _tbTrackMediaFiles.Lines) {
                var newTrack = new Track();
                newTrack.Album = _tbCommonAlbum.Text;
                newTrack.Artist = _tbCommonArtist.Text;
                newTrack.Name = Path.GetFileNameWithoutExtension(trackMediaFile);
                newTrack.Url = trackMediaFile;
                //create initial cue
                newTrack.Cues.Add(
                    new Cue {
                        Description = "Intro (autogenerated)",
                        Shortcut = "1",
                        Time = 0
                    });
                compilation.Tracks.Add(newTrack);
            }

            Core.Model.Instance.Compilation = compilation; //assign to model.
            DialogResult = DialogResult.OK;
            Close();
        }

        private void _btnBrowserForTracks_Click(object sender, EventArgs e) {
            var otfd = new OpenTrackFileDialog();
            if (otfd.ShowDialog(true) == DialogResult.OK) //pressed ok by the user?
            {
                _tbTrackMediaFiles.Lines = otfd.FileNames;
            }
        }

        private void wizardControl1_CancelButtonClick(object sender, EventArgs e) {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}